package com.jiyang.jyandroid.test01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by jy on 2016/10/10.
 * 可拖动的小球
 */

public class DrawView extends View {
    //小球初始位置
    private float currentX = 50;
    private float currentY = 50;
    private float radius = 20;   //小球半径
    //定义并创建画笔
    private Paint p = new Paint();

    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        p.setColor(Color.BLUE); //设置画笔颜色
        canvas.drawCircle(currentX, currentY, radius, p); //绘制小球
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //修改小球坐标
        currentX = event.getX();
        currentY = event.getY();
        //通知小球，重新绘制自己
        invalidate();
        //返回true表明该处理方法已经处理事件
        return true;
    }

    /**
     * 动态设置小球半径
     *
     * @param radius 小球半径
     */
    public void setRadio(int radius) {
        //修改半径
        this.radius = radius;
        //通知小球，重新绘制自己
        invalidate();
    }
}
